layui.use(['form', 'jquery', 'layer', 'table', 'laydate', 'laypage', 'upload'], function() {
	var form = layui.form;
	var $ = layui.$;
	var table = layui.table;
	var layer = layui.layer;
	var laydate = layui.laydate;
	var laypage = layui.laypage;
	var upload = layui.upload;
	var order_price = 0;
	console.info(select_goods_info);
	// 处理商品
	var goods_table_filter = 'goods_list',
		goods_form_filter = 'goods_search_form',
		select_goods_table_filter = 'select_goods_list',
		select_goods_form_filter = 'select_goods_search_form';
	var select_attr_handle = function(obj, res) {
		var goods_sku = obj.parents('tr').find('td[data-field="goods_sku"]').attr('data-val'),
			sale_price_html = obj.parents('tr').find('td[data-field="sale_price"]'),
			sale_attr,
			sale_attr_text = obj.text().split('+');
		for (var i in res.data) {
			var e = res.data[i];
			if (e.goods_sku == goods_sku) {
				sale_attr = e.sale_attr;
				sale_price_info = e.sale_price_info;
				break;
			}
		}
		// 构建销售属性页面
		var create_sale_html = function(sale_attr_info, value) {
			var temp = sale_attr_info.sale_attr_value.split(','),
				input_html = '';
			for (var i in temp) {
				var e = temp[i];
				if (e == value) {
					input_html = input_html + '<input type="radio" name="' + sale_attr_info.sale_attr_name + '" checked value="' +
						e + '" title="' + e + '">';
				} else {
					input_html = input_html + '<input type="radio" name="' + sale_attr_info.sale_attr_name + '"  value="' + e +
						'" title="' + e + '">';
				}
			}
			var html = '<div class="layui-form-item">' +
				'<label class="layui-form-label">' + sale_attr_info.sale_attr_name + '</label>' +
				'<div class="layui-input-inline" style="width: 500px;">' +
				input_html +
				'</div></div>';
			return html;
		}
		$('#select_sale_attr form').html('');
		for (var i in sale_attr) {
			var e = sale_attr[i],
				html = create_sale_html(e, sale_attr_text[i]);
			$('#select_sale_attr form').append(html);
		}
		form.render('radio');
		var select_attr_page_id = layer.open({
			content: $('#select_sale_attr'),
			id: 'select_attr_page',
			type: 1,
			anim: 2,
			title: '选择属性',
			closeBtn: true,
			shadeClose: true,
			area: '800px',
			shade: 0.1,
			btn: ['确定选择'],
			yes: function(index, layero) {
				var sale_attr_html = '',
					sale_attr_price = '';
				for (var i in sale_attr) {
					var e = sale_attr[i],
						value = $('input[name="' + e.sale_attr_name + '"]:checked').val();
					if (value == undefined || value == '') {
						layer.msg('请选择' + e.sale_attr_name + '属性', {
							icon: 2,
							anim: 6
						});
						return false;
					}
					sale_attr_html = sale_attr_html == '' ? value : sale_attr_html + '+' + value;
					if (e.is_sale_price == 1) {
						sale_attr_price = sale_attr_price == '' ? value : sale_attr_price + '+' + value;
					}
				}
				var temp = {
					goods_sku: goods_sku,
					field: 'sale_attr_html',
					value: sale_attr_html
				};
				select_cache(temp);
				var temp = {
					goods_sku: goods_sku,
					field: 'sale_attr',
					value: sale_attr_price
				};
				select_cache(temp);
				// 重置价格
				if (sale_attr_price != '') {
					var sale_price = 0;
					for (var i in sale_price_info.data) {
						var e = sale_price_info.data[i];
						if (e.sale_attr_price_name == sale_attr_price) {
							sale_price = e.sale_price;
							break;
						}
					}
					var temp = {
						goods_sku: goods_sku,
						field: 'sale_price',
						value: sale_price
					};
					select_cache(temp);

				}
				layer.close(select_attr_page_id);
			}
		})
	};
	// 处理商品选择缓存
	var select_cache = function(obj) {
		var new_json = new Array(),
			have_flag = 1,
			tr = $('div[lay-id="goods_list"]').find('div.layui-table-body').find('table tbody').find(
				'tr td[data-field="goods_sku"][data-val="' + obj.goods_sku + '"]').parent('tr');
		for (var i in select_goods_info) {
			var e = select_goods_info[i];
			if (obj.goods_sku == e.goods_sku) {
				switch (obj.field) {
					case 'num':
						select_goods_info[i].num = obj.value;
						tr.find('td[data-field="' + obj.field + '"]').attr('data-val', obj.value);
						tr.find('td[data-field="' + obj.field + '"]').find('div').text(obj.value);
						break;
					case 'sale_attr_html':
						select_goods_info[i].sale_attr_html = obj.value;
						tr.find('td[data-field="sale_attr"]').find('div').find('span').text(obj.value);
						break;
					case 'sale_attr':
						select_goods_info[i].sale_attr = obj.value;
						break;
					case 'sale_price':
						select_goods_info[i].sale_price = obj.value;
						tr.find('td[data-field="' + obj.field + '"]').attr('data-val', obj.value);
						tr.find('td[data-field="' + obj.field + '"]').find('div').text(obj.value);
						break;
					default:
						// 删除
						select_goods_info.splice(i, 1);
						break;
				}
				have_flag = 0;
			}
		};

		if (have_flag == 1) {
			// 存在标志，不存在 添加;
			select_goods_info.push(obj);
		}
		order_price = 0;
		for (var i in select_goods_info) {
			var e = select_goods_info[i];
			if (e.sale_price != undefined) {
				order_price = order_price + (e.num * Math.ceil(e.sale_price * 100)) / 100;
			}
		}
		$('form[lay-filter="tpl_order"]').find('#order_price').html(Math.abs(order_price).toFixed(2));
		// 修改订单数据
		if (obj.goods_sku != undefined) {
			var post_data = {
				order_no: order_no,
				event: 'edit_cart',
				post_data: obj
			};
			send_info(order_edit_url, post_data, function(data) {
				parent.layui.table.reload('order_list');
			})
		}
	}
	// 已选择的商品编号
	var goods_skus_input = $('form[lay-filter="tpl_order"]').find('input[name="goods_skus"]');
	// 已选择
	table.render({
		elem: '#' + goods_table_filter,
		id: goods_table_filter,
		url: goods_list_url,
		method: 'post',
		where: {
			_o: 'goods_id',
			_b: 'desc',
			select_goods_skus: goods_skus_input.val(),
			site_id: site_id
		},
		cols: [goods_cols],
		autoSort: false,
		toolbar: shopping_state == 2 ? false : '#goodsToolbar',
		defaultToolbar: [],
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		},
		done: function(res, curr, count) {
			for (var i in res.data) {
				var e = res.data[i],
					flag = 0;
				for (var j in select_goods_info) {
					var e1 = select_goods_info[j];
					if (e.goods_sku == e1.goods_sku) {
						flag = 1;
					}
				}
				if (flag == 0) {
					if (e.sale_attr.length == 0) {
						var temp = {
							goods_sku: e.goods_sku,
							sale_price: e.sale_price,
							num: 1,
							sale_attr_html: '默认属性',
							sale_attr: ''
						};
						select_cache(temp);
					} else {
						var temp = {
							goods_sku: e.goods_sku,
							sale_price: e.sale_price,
							num: 1,
							sale_attr_html: '请选择',
							sale_attr: ''
						};
						select_cache(temp);
					}
				} else {
					// 初始化
					var temp = {};
					select_cache(temp);
				}
			}
			if (order_state != 1) {
				$('div[lay-id="goods_list"]').find('.select-sale-attr').removeClass('right-select');
			}
			// 选择销售属性
			$('div[lay-id="goods_list"]').on('click', '.select-sale-attr', function() {
				if (order_state == 1) {
					select_attr_handle($(this), res);
				}
			})
			// 选择销售属性结束
			// 赋值
			var tr = $('div[lay-id="goods_list"] div.layui-table-body table tbody tr');
			tr.each(function() {
				for (var i in select_goods_info) {
					var e = select_goods_info[i];
					if ($(this).find('td[data-field="goods_sku"]').attr('data-val') == e.goods_sku) {
						$(this).find('td[data-field="sale_price"]').attr('data-val', e.sale_price);
						$(this).find('td[data-field="num"]').attr('data-val', e.num);
						$(this).find('td[data-field="sale_price"]').find('div').html(e.sale_price);
						$(this).find('td[data-field="num"]').find('div').html(e.num);
						if (e.sale_attr_html != '') {
							$(this).find('td[data-field="sale_attr"]').find('div span').html(e.sale_attr_html);
						}
						if (order_state >= 2 && e.pack_state == 1) {
							$(this).find('td[data-field="goods_id"]').find('div').hide();
						}
						$(this).find('td[data-field="returned"]').find('div').hide();
						if ((order_state == 3 || order_state == 4) && e.pack_state == 1) {
							$(this).find('td[data-field="returned"]').find('div').show();
							switch (e.returned_state) {
								case 1:
									break;
								case 2:
									$(this).find('td[data-field="returned"]').find('div').html('退货中..');
									break;
								case 3:
									$(this).find('td[data-field="returned"]').find('div').html('退货完成');
									break;
								case 4:
									$(this).find('td[data-field="returned"]').find('div').html('换货中..');
									break;
								case 5:
									$(this).find('td[data-field="returned"]').find('div').html('换货完成');
									break;
								case -1:
									$(this).find('td[data-field="returned"]').find('div').html('取消退换货');
									break;
								default:
									break;
							}
						}
					}
				}
			})

		}
	});
	// 未选择
	table.render({
		elem: '#' + select_goods_table_filter,
		id: select_goods_table_filter,
		url: goods_list_url,
		mothod: 'post',
		where: {
			_o: 'goods_id',
			_b: 'desc',
			sale_state: 1,
			audit_state: 1,
			site_id: site_id,
			no_select_goods_skus: goods_skus_input.val()
		},
		cols: [select_goods_cols],
		autoSort: false,
		initSort: {
			'field': 'goods_id',
			'type': 'desc'
		},
		height: 'full-200',
		page: {
			layout: ['count', 'first', 'prev', 'page', 'next', 'last', 'skip'],
			limit: 30,
			theme: '#393d49',
			groups: 8
		}
	})
	// 监听点击
	table.on('row(' + select_goods_table_filter + ')', function(obj) {
		var goods_sku = obj.data.goods_sku,
			get_goods_skus_form = $('form[lay-filter="tpl_order"]').find('input[name="goods_skus"]');
		if (get_goods_skus_form.val() == '') {
			get_goods_skus_form.val(goods_sku);
		} else {
			get_goods_skus_form.val(get_goods_skus_form.val() + ',' + goods_sku);
		}
		table.reload(goods_table_filter, {
			where: {
				select_goods_skus: goods_skus_input.val()
			}
		});
		obj.del();
	})
	// 确认收货
	$(document).on('click', 'button[lay-filter="submit_receipt"]', function() {
		var express_id = $(this).attr('data-id');
		layer.confirm('确认要收货吗？', {
			icon: 3,
			title: '确认操作'
		}, function(index) {
			var temp = {
					order_state: 4,
					express_id: express_id
				},
				post_data = {
					order_no: order_no,
					event: 'edit_state',
					post_data: temp
				};
			send_info(order_edit_url, post_data, function(data) {
				parent.layui.table.reload('order_list');
				push_henan(order_no);
				//location.reload();
			})
		})
	})
	table.on('toolbar(' + goods_table_filter + ')', function(obj) {
		var get_ids = table.checkStatus(obj.config.id);
		switch (obj.event) {
			case 'addGoodsToCart':
				layer.open({
					content: $('#select_goods'),
					type: 1,
					anim: 2,
					title: '选择商品',
					closeBtn: true,
					shadeClose: true,
					area: '85%',
					shade: 0.1
				})
				break;
			case 'shoppingGoods':
				if (get_ids.data.length == 0) {
					layer.msg('请选择发货商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				var new_goods_skus = new Array();
				for (var i in select_goods_info) {
					var e = select_goods_info[i];
					for (var j in get_ids.data) {
						var e1 = get_ids.data[j];
						if (e.goods_sku == e1.goods_sku) {
							if (e.pack_state == 2) {
								new_goods_skus.push(e.goods_sku);
							}
						}
					}
				}
				if (new_goods_skus.length == 0) {
					layer.msg('请选择发货商品', {
						icon: 2,
						anim: 6
					});
					return false;
				}
				// 获取仓库
				send_info(depot_create_no_url, {}, function(data) {
					var depot_ul = $('form[lay-filter="tpl_express"]').find('select[lay-filter="depot"]');
					$('form[lay-filter="tpl_express"]').find('input[name="order_no"]').val(order_no);
					$('form[lay-filter="tpl_express"]').find('input[name="cart_goods_skus"]').val(new_goods_skus);
					$('form[lay-filter="tpl_express"]').find('input[name="express_no"]').val(data.count);
				})
				layer.open({
					content: $('#express'),
					type: 1,
					anim: 2,
					title: '订单产品发货',
					closeBtn: true,
					shadeClose: true,
					area: ['650px', '510px'],
					shade: 0.1
				})

				break;
		}
		return false;
	});
	// 确认发货
	form.on('submit(submit_express)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="tpl_express"]').attr('action');
		layer.confirm('确认对选择的商品发货吗?', {
			icon: 3,
			title: '确认信息'
		}, function(index) {
			send_info(url, field, function(data) {
				if (data.code == 0) {
					parent.layui.table.reload('order_list');
					push_henan(order_no);
					parent.layer.msg('发货成功,需要手动刷新页面.',{
						icon:1,
						anim:6
					})
					//location.reload();
				} else {
					layer.confirm(data.msg, {
						icon: 2,
						title: '发货失败'
					})
				}
			})
		})
	})
	// 创建合同
	$('.create_contract').on('click', function() {
		push_neimeng_model('createContract', order_no);
	})
	// 查看合同
	$('.view_contract').on('click', function() {
		push_henan_model('contractDetail', order_no);
	})
	// 查看验收状态
	$('.view_check').on('click', function() {
		push_henan_model('getOrderCheck', order_no);
	})
	// 发送合同
	$('.send_contract').on('click', function() {
		push_neimeng_model('sendContract', order_no);
	})
	// 确认申请退换货
	form.on('submit(submit_returned)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="tpl_returned"]').attr('action');
		send_info(url, field, function(data) {
			var returned_state = field.returned_type == 1 ? 2 : 4;
			for (var i in select_goods_info) {
				if (select_goods_info[i]['goods_sku'] == field.goods_sku) {
					select_goods_info[i]['returned_state'] = returned_state;
				}
			}
			table.reload(goods_table_filter);
			layer.closeAll('page');
		})
	})

	table.on('tool(' + goods_table_filter + ')', function(obj) {
		var data = obj.data,
			num = $(this).parents('tr').find('td[data-field="num"]').attr('data-val'),
			goods_skus = $('form[lay-filter="tpl_order"]').find('input[name="goods_skus"]').val().split(',');
		switch (obj.event) {
			// 申请退换货
			case 'returned':
				$('#returned_box').find('input[name="goods_sku"]').val(data.goods_sku);
				$('#returned_box').find('input[name="cart_id"]').val(data.cart_id);
				$('#returned_box').find('input[name="returned_num"]').attr('data-cart_num', num).val(num);
				layer.open({
					content: $('#returned_box'),
					type: 1,
					anim: 2,
					title: '申请退换货',
					closeBtn: true,
					shadeClose: true,
					area: ['650px', '510px'],
					shade: 0.1
				})
				break;
			case 'remove':
				var temp = {
					goods_sku: data.goods_sku
				};
				var where = new Array();
				for (var i in goods_skus) {
					if (goods_skus[i] != data.goods_sku) {
						where.push(goods_skus[i]);
					}
				}
				var input_value = where.join(',');
				$('form[lay-filter="tpl_order"]').find('input[name="goods_skus"]').val(input_value);
				obj.del();
				select_cache(temp);
				table.reload(select_goods_table_filter, {
					where: {
						no_select_goods_skus: input_value
					}
				});
				break;
			default:
				break;
		}
	})
	// 监听商品修改价格和数量
	table.on('edit(' + goods_table_filter + ')', function(obj) {
		var field = obj.field,
			value = obj.value,
			num_value = 1,
			tr = obj.tr;
		if (tr.find('td[data-field="' + field + '"]').attr('data-val') != '') {
			num_value = tr.find('td[data-field="' + field + '"]').attr('data-val');
		}
		if (field == 'num') {
			var reg = /^\d+$/;
			if (!reg.test(value) || value <= 0) {
				layer.msg('数量格式错误', {
					icon: 2
				});
				tr.find('td[data-field="' + field + '"]').html('<div class="layui-table-cell laytable-cell-' + tr.find(
					'td[data-field="' + field + '"]').attr('data-key') + '">' + num_value + '</div>');
				return false;
			}
		} else {
			var reg = /^\d+(\.\d+)?$/;
			if (!reg.test(value)) {
				layer.msg('金额格式错误', {
					icon: 2
				});
				tr.find('td[data-field="' + field + '"]').html('<div class="layui-table-cell laytable-cell-' + tr.find(
					'td[data-field="' + field + '"]').attr('data-key') + '">' + num_value + '</div>');
				return false;
			}
		}
		var temp = {
			goods_sku: obj.data.goods_sku,
			field: field,
			value: value
		};
		select_cache(temp);
	});

	// 交货日期
	var now = new Date(); //今天
	var tomo = new Date((now / 1000 + 86400 * 2) * 1000); //明天
	laydate.render({
		elem: 'input[name="delivery_date"]',
		min: +2,
		value: default_date,
		done: function(value, date, endDate) {
			var temp = {
				delivery_date: value
			};
			var post_data = {
				order_no: order_no,
				event: 'edit_delivery_date',
				post_data: temp
			};
			send_info(order_edit_url, post_data, function(data) {
				parent.layui.table.reload('order_list');
				layer.msg('保存成功', {
					icon: 1,
					anim: 6
				});
			})
		}
	});
	// 初始化城市
	var initSelectEvent = function(obj) {
		var province = obj["province"],
			city = obj["city"],
			area = obj["district"];
		var province_ul = $('form[lay-filter="tpl_shopping"]').find('select[name="province"]');
		province_ul.html('<option value="">请选择省份</option>');
		for (var i in province) {
			var e = province[i];
			province_ul.append('<option value="' + e.id + '">' + e.name + '</option>');
		}
		// 选择城市
		form.render('select');
		form.on('select(province)', function(data) {
			var city_list = city[data.value];
			var city_ul = $('form[lay-filter="tpl_shopping"]').find('select[name="city"]');
			city_ul.html('<option value="">请选择</option>');
			for (var i in city_list) {
				var e = city_list[i];
				city_ul.append('<option value="' + e.id + '">' + e.name + '</option>');
			}
			form.render('select');
		});
		form.on('select(city)', function(data) {
			var area_list = area[data.value];
			var area_ul = $('form[lay-filter="tpl_shopping"]').find('select[name="area"]');
			area_ul.html('<option value="">请选择</option>');
			for (var i in area_list) {
				var e = area_list[i];
				area_ul.append('<option value="' + e.id + '">' + e.name + '</option>');
			}
			form.render('select');
		})
	}
	var ajaxConfig = {
		url: "/static/js/jqcity/allcity.js",
		dataType: "jsonp",
		jsonpCallback: "callback",
		success: initSelectEvent
	};
	$.ajax(ajaxConfig);
	// 订单完成
	$('form[lay-filter="tpl_order"]').on('click', 'button[lay-filter="over_order"]', function() {
		layer.confirm('订单完成后将不能退换货，确认完成吗？', {
			icon: 3,
			title: '确认操作'
		}, function(index) {
			var temp = {
					order_state: 5
				},
				post_data = {
					order_no: order_no,
					event: 'edit_state',
					post_data: temp
				};
			send_info(order_edit_url, post_data, function(data) {
				parent.layui.table.reload('order_list');
				location.reload();
			})
		})

	})
	var push_henan = function(order_no) {
		layer.msg('保存成功，准备推送数据', {
			icon: 1
		});
		layer.open({
			type: 2,
			anim: -1,
			title: !1,
			closeBtn: !1,
			offset: 'r',
			shade: 0.1,
			shadeClose: !0,
			area: '400px',
			skin: "layui-anim layui-anim-rl layui-layer-adminRight",
			content: push_henan_url + '?order_no=' + order_no
		})
	}
	var push_henan_model = function(model, order_no) {
		layer.open({
			type: 2,
			anim: -1,
			title: !1,
			closeBtn: !1,
			offset: 'r',
			shade: 0.1,
			shadeClose: !0,
			area: '400px',
			skin: "layui-anim layui-anim-rl layui-layer-adminRight",
			content: push_henan_url + '?order_no=' + order_no + '&model=' + model
		})
	}
	// 确认订单
	$('form[lay-filter="tpl_order"]').on('click', 'button[lay-filter="confirm_order"]', function() {
		layer.confirm('确认订单吗？', {
			icon: 3,
			title: '确认操作'
		}, function(index) {
			var temp = {
					order_state: 2
				},
				post_data = {
					order_no: order_no,
					event: 'edit_state',
					post_data: temp
				};
			send_info(order_edit_url, post_data, function(data) {
				push_henan(order_no);
				parent.layui.table.reload('order_list');
				//location.reload();

			})
		})
	})
	// 取消订单
	$('form[lay-filter="tpl_order"]').on('click', 'button[lay-filter="cancel_order"]', function() {
		layer.confirm('确认取消订单吗？', {
			icon: 3,
			title: '确认操作'
		}, function(index) {
			var temp = {
					order_state: -1,
					cancel_type: 2
				},
				post_data = {
					order_no: order_no,
					event: 'edit_state',
					post_data: temp
				};
			send_info(order_edit_url, post_data, function(data) {
				push_henan(order_no);
				parent.layui.table.reload('order_list');
				//location.reload();
			})
		})

	})
	// 关闭页面
	$('#close_page').on('click', function() {
		parent.layer.closeAll('iframe');
	})
	var get_shopping = function(shopping_id) {
		send_info(shopping_find_url, {
			shopping_id: shopping_id
		}, function(data) {
			var e = data.data;
			var ul = $('form[lay-filter="tpl_order"]').find('#shopping_list');
			ul.html('<div><input type="radio" value="' + e.shopping_id +
				'" checked name="shopping_id" lay-skin="primary" title="' + e.province_name + ' ' + e.city_name + ' ' + e.area_name +
				' ' + e.street + ' （' + e.contacts + ' 收）<span class=phone>' + e.phone +
				'</span><span class=edit_shopping>修改</span>" /></div>');
			form.render('radio');
		})
	}
	// 保存收货地址
	form.on('submit(submit_shopping)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="tpl_shopping"]').attr('action');
		send_info(url, field, function(data) {
			if (data.code == 0) {
				layer.msg('保存成功', {
					icon: 1
				});
				get_shopping(field.shopping_id);
				var post_data = {
					order_no: order_no,
					event: 'edit_shopping',
					post_data: field
				};
				send_info(order_edit_url, post_data, function(data) {
					return true;
				})
				layer.closeAll('page');
			} else {
				layer.msg(data.msg, {
					icon: 2,
					anim: 6
				});
			}
		})
		return false;
	})
	// 修改配送地址
	$('#shopping_list').on('click', '.edit_shopping', function() {
		var shopping_id = $(this).parents('div.layui-form-radio').prev('input[type="radio"]').val();
		send_info(shopping_find_url, {
			shopping_id: shopping_id
		}, function(data) {
			form.val('tpl_shopping', data.data);
			// 省份
			$('select[name="province"]').next('div.layui-form-select').find('dl').find('dd[lay-value="' + data.data.province +
				'"]').trigger('click');
			// 城市
			$('select[name="city"]').val(data.data.city);
			$('select[name="city"]').next('div.layui-form-select').find('dl').find('dd[lay-value="' + data.data.city +
				'"]').trigger('click');
			// 区县
			$('select[name="area"]').val(data.data.area);
			form.render('select');
			layer.open({
				content: $('#shopping'),
				type: 1,
				anim: 2,
				title: '修改配送地址',
				closeBtn: true,
				shadeClose: true,
				area: ['800px', '500px'],
				shade: 0.1
			})
		});

	})
	// 监听支付方式修改
	form.on('radio(pay_type)', function(data) {
		var temp = {
			pay_type: data.value
		};
		var post_data = {
			order_no: order_no,
			event: 'edit_pay_type',
			post_data: temp
		};
		send_info(order_edit_url, post_data, function(data) {
			return true;
		})
	})

	// 处理开发票信息
	form.on('radio(is_invoice)', function(data) {
		if (data.value == 2) {
			$('#invoice_info').show(300);
		} else {
			$('#invoice_info').hide(300);
		}
	})
	if (invoice_info_json.invoice_type == 3) {
		$('.invoice_type3').show();
	} else {
		$('.invoice_type3').hide();
	}
	// 监听发票类型
	var edit_invoice = function(temp) {
		var post_data = {
			order_no: order_no,
			event: 'edit_invoice',
			post_data: temp
		};
		if (temp.value != invoice_info_json[temp.field]) {
			invoice_info_json[temp.field] = temp.value;
			send_info(order_edit_url, post_data, function(data) {
				layer.msg('保存成功', {
					icon: 1,
					anim: 6
				});
				return true;
			})
		}
	}
	form.on('radio(invoice_type)', function(data) {
		if (data.value == 3) {
			$('.invoice_type3').show(300);
		} else {
			$('.invoice_type3').hide(300);
		}
		var temp = {
			field: 'invoice_type',
			value: data.value,
			invoice_id: invoice_info_json.invoice_id
		};
		edit_invoice(temp);
	})
	$('#invoice_info').on('blur', 'input[type="text"]', function() {
		var field = $(this).attr('name'),
			value = $(this).val(),
			temp = {
				field: field,
				value: value,
				invoice_id: invoice_info_json.invoice_id
			};
		edit_invoice(temp);
	})
	// 开具发票
	form.on('submit(submit_invoice)', function(data) {
		var field = {
			order_no: order_no,
			invoice_id: invoice_info_json.invoice_id,
			site_id: site_id,
			state: 1
		};
		layer.confirm('确认要开具发票吗？', {
			icon: 3,
			title: '确认开票'
		}, function(index) {
			send_info(save_alone_invoice_url, field, function(data) {
				parent.layui.table.reload('order_list');
				parent.layer.msg('发票开具成功', {
					icon: 1,
					anim: 6
				});
				push_henan_model('sendInvoice', order_no);
				//location.reload();
			})
		})
	})
	// 上传发票凭证
	var upload_invoice = upload.render({
		elem: '.up_invoice',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'image/*,application/pdf',
		exts: 'jpg|png|gif|bmp|jpeg|pdf',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			var e = this.item,
				invoice_proof = res.data.upload_id,
				invoice_id = e.context.dataset.invoice_id;
			var field = {
				invoice_id: invoice_id,
				field: 'invoice_proof',
				value: invoice_proof
			};
			send_info(save_invoice_url, field, function(data) {
				parent.layui.table.reload('order_list');
				parent.layer.msg('发票凭证上传成功', {
					icon: 1,
					anim: 6
				});
				layer.closeAll('loading');
				location.reload();
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});
	// 上传配送信息
	var upload_express = upload.render({
		elem: '.up_express',
		url: upload_uri,
		accept: 'file',
		acceptMime: 'image/*,application/pdf',
		exts: 'jpg|png|gif|bmp|jpeg|pdf',
		choose: function() {
			layer.load(1);
		},
		done: function(res) {
			console.info(res);
			var e = this.item,
				express_proof = res.data.upload_id,
				express_id = e.context.dataset.express_id;
			var field = {
				express_id: express_id,
				field: 'express_proof',
				value: express_proof
			};
			send_info(save_express_url, field, function(data) {
				parent.layui.table.reload('order_list');
				parent.layer.msg('发票物流上传成功', {
					icon: 1,
					anim: 6
				});
				layer.closeAll('loading');
				push_henan_model('sendExpress', order_no);
				//location.reload();
			})
		},
		error: function() {
			layer.closeAll('loading');
			layer.msg('上传错误', {
				icon: 2,
				anim: 6
			});
			return false;
		}
	});
	$('.get_invoice').on('click',function(){
		layer.confirm('发票确认已收到吗?', {
			icon: 3,
			title: '提示'
		}, function(index) {
			push_henan_model('getInvoice', order_no);
		});
	})
	// 修改备注
	$('textarea[name="remark"]').on('blur', function() {
		var value = $(this).val();
		if (value != remark_info) {
			var temp = {
				remark: value
			};
			var post_data = {
				order_no: order_no,
				event: 'edit_remark',
				post_data: temp
			};
			send_info(order_edit_url, post_data, function(data) {
				layer.msg('保存成功', {
					icon: 1,
					anim: 6
				});
				remark_info = value;
			})
		}
	})
	// 初始化安装日期
	laydate.render({
		elem: 'input[name="install_time"]',
		min: +1
	});
	// 支付信息
	$(document).on('click', 'button[lay-filter="submit_pay"]', function() {
		layer.open({
			content: $('#add_pay_info'),
			type: 1,
			anim: 2,
			title: '录入支付信息',
			closeBtn: true,
			shadeClose: true,
			area: '650px',
			shade: 0.1
		})
	})
	// 确定支付信息
	form.on('submit(submit_add_pay_info)', function(data) {
		var field = data.field;
		field.order_no = order_no;
		var url = $('form[lay-filter="tpl_add_pay_info"]').attr('action');
		send_info(url, field, function(data) {
			parent.layer.msg('订单支付成功', {
				icon: 1,
				anim: 6
			});
			parent.layui.table.reload('order_list');
			push_henan_model('payInfo', order_no);
			//location.reload();
		})
	});
	// 录入安装信息
	$(document).on('click', 'button[lay-filter="submit_install_info"]', function() {
		var cart_id = $(this).attr('data-cart_id');
		$('form[lay-filter="tpl_add_install_info"]').find('input[name="cart_id"]').val(cart_id);
		layer.open({
			content: $('#add_install_info'),
			type: 1,
			anim: 2,
			title: '录入安装信息',
			closeBtn: true,
			shadeClose: true,
			area: '650px',
			shade: 0.1
		})
	})
	// 确认录入安装信息
	form.on('submit(submit_add_install)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="tpl_add_install_info"]').attr('action');
		send_info(url, field, function(data) {
			parent.layer.msg('安装信息录入成功', {
				icon: 1,
				anim: 6
			});
			location.reload();
		})
	});

	// 执行工程师
	$(document).on('click', 'button[lay-filter="submit_engireer_info"]', function() {
		var cart_id = $(this).attr('data-cart_id');
		$('form[lay-filter="tpl_add_install_engineer"]').find('input[name="cart_id"]').val(cart_id);
		layer.open({
			content: $('#add_install_engineer'),
			type: 1,
			anim: 2,
			title: '执行工程师',
			closeBtn: true,
			shadeClose: true,
			area: '650px',
			shade: 0.1
		})
	});
	// 确定执行工程师
	form.on('submit(submit_add_engineer)', function(data) {
		var field = data.field;
		var url = $('form[lay-filter="tpl_add_install_engineer"]').attr('action');
		send_info(url, field, function(data) {
			parent.layer.msg('工程师执行成功', {
				icon: 1,
				anim: 6
			});
			location.reload();
		})
	});
	// 安装完成
	$(document).on('click', 'button[lay-filter="submit_over_info"]', function() {
		var cart_id = $(this).attr('data-cart_id');
		var url = $('form[lay-filter="tpl_add_install_engineer"]').attr('action');
		var field = {
			cart_id: cart_id,
			step: 3
		};
		layer.confirm('确认安装完成吗?', {
			icon: 3,
			title: '提示'
		}, function(index) {
			send_info(url, field, function(data) {
				parent.layer.msg('安装设置完成', {
					icon: 1,
					anim: 6
				});
				location.reload();
			})
		});
	})
	// 表单验证
	form.verify({
		gtzero: function(value, item) {
			if (value < 1) {
				return '退换货数量不能小于1';
			}
		},
		ltcart: function(value, item) {
			var cart_num = item.getAttribute('data-cart_num');
			if (cart_num == undefined) {
				maxValue = 1;
			} else {
				maxValue = cart_num;
			}
			if (parseInt(value) > parseInt(maxValue)) {
				return '退换货数量不能大于已购买数量';
			}
		}
	});
	// 商品 查询
	$('form[lay-filter="' + select_goods_form_filter + '"]').on('input propertychange', 'input[type="text"]', function() {
		if ($(this).prop('comStart')) return;
		var value = $(this).val();
		var name = $(this).attr('name');
		var where = {};
		where[name] = value;
		table.reload(select_goods_table_filter, {
			where: where
		});
	}).on('compositionstart', 'input[type="text"]', function() {
		$(this).prop('comStart', true);
	}).on('compositionend', 'input[type="text"]', function() {
		$(this).prop('comStart', false);
		$(this).trigger('input');
	});
	var select_search = function(field, value) {
		var where = {};
		where[field] = value;
		table.reload(select_goods_table_filter, {
			where: where
		});
	};
	// 获取品牌
	var get_goods_brand = function(category_sign) {
		var e = $('#brand_sign');
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		form.render('select');
		var post_data = {
			is_select: 'yes',
			site_id: site_id,
			category_sign: category_sign,
			_o: 'brand_id',
			_b: 'desc',
			page: 1,
			limit: 100
		};
		send_info(brand_select_uri, post_data, function(data) {
			e.html('<option value="">请选择</option>');
			if (data.count > 0) {
				for (var i in data.data) {
					var temp = data.data[i];
					if (goods_form_value.brand_sign == temp.brand_sign) {
						var op_html = '<option selected value="' + temp.brand_sign + '">' + temp.brand_name + '</option>';
					} else {
						var op_html = '<option value="' + temp.brand_sign + '">' + temp.brand_name + '</option>';
					}
					e.append(op_html);
				}
			};
			e.prop('disabled', false);
			form.render('select');
		})
	};
	var goods_form_value = {
		category_sign1: '',
		category_sign2: '',
		category_sign3: ''
	};
	// 初始化分类
	var get_goods_category = function(parent_sign, filter, value) {
		var e = $('#' + filter);
		e.html('');
		e.append('<option value="">加载中...</option>');
		e.prop('disabled', true);
		var id = e.attr('id');
		form.render('select');
		var post_data = {
			parent_sign: parent_sign,
			site_id: site_id,
			audit_state: 1
		}
		send_info(category_select_uri, post_data, function(data) {
			e.html('<option value="">请选择分类</option>');
			for (var i in data.data) {
				var temp = data.data[i];
				if (value == temp.category_sign) {
					var op_html = '<option selected value="' + temp.category_sign + '">' + temp.category_name + '</option>';
				} else {
					var op_html = '<option value="' + temp.category_sign + '">' + temp.category_name + '</option>';
				}
				e.append(op_html);
			}
			e.prop('disabled', false);
			form.render('select');
			if (value != undefined) {
				if (id == 'category_search1') {
					get_goods_category(value, 'category_search2', goods_form_value.category_sign2);
					$('#category_search3').html('');
					$('#goods_brand_search').html('');
					get_goods_brand(value);
				} else if (id == 'category_search2') {
					get_goods_category(value, 'category_search3', goods_form_value.category_sign3);
					get_goods_brand(value);
				} else {
					get_goods_brand(value);
				}
			}
		})
	}
	var fliter1 = 'category_search1';
	// 分类联动
	form.on('select', function(data) {
		var value = data.value;
		var e = data.elem;
		var id = $(e).attr('id');
		switch (id) {
			case 'category_search1':
				get_goods_category(value, 'category_search2', goods_form_value.category_sign2);
				$('#category_search3').html('');
				$('#brand_sign').html('');
				get_goods_brand(value);
				select_search('category_sign1', value);
				break;
			case 'category_search2':
				get_goods_category(value, 'category_search3', goods_form_value.category_sign3);
				get_goods_brand(value);
				select_search('category_sign2', value);
				break;
			case 'category_search3':
				get_goods_brand(value);
				select_search('category_sign3', value);
				break;
			case 'site_id':
				goods_select_site_id = value;
				select_search('site_id', value);
				break;
			case 'brand_sign':
				select_search('brand_sign', value);
				break;
			default:
				break;
		}
	});
	get_goods_category(0, 'category_search1', goods_form_value.category_sign1);

})
